﻿/**
 * @fileOverview 文字跑马灯
 * @copyright (c) 2012 (condenast.com.cn)
 * @author jianjun.wang@condenast.com.cn
 * @date  2012.7.9
 * @version 1.0
 */
if(typeof CNC=="undefined" || !CNC){
	var CNC = {};
}
if(typeof CNC.ui=="undefined" || !CNC.ui){
	CNC.ui = {};
}
/**
 * @class 文字跑马灯
 * @param {object} config
   {<br/>
		speed:(int)滚动速度，默认为10毫秒,<br/>
		autoStart:(boolean)自动滚动，默认为true,<br/>
		scrollObjId:(string)滚动容器id,<br/>
   }
 */
CNC.ui.Marquee = function(config){
	this.config = {
		speed: 10,
		autoStart:true,
		scrollObjId:null
	}

	$.extend(this.config, config);
	this.scrollStop = false;	
	
	this.scrollObj = $("#" + this.config.scrollObjId);
	this.content1 = $("#" + this.config.scrollObjId + "-content1");
	this.content2 = $("#" + this.config.scrollObjId + "-content2");
  
	this.init();
}
CNC.ui.Marquee.prototype = {
	/**
	 * @ignore		
	 */
	init:function(){
		var self = this;
		self.content2.html( self.content1.html() );

		self.scrollObj.mouseenter(function(){
			self.mouseOver();
		});
		self.scrollObj.mouseleave(function(){
			self.mouseOut();
		});
		if(self.config.autoStart){
			self.start();
		}
	},
	/**
	 * 开始滚动		
	 */
	start:function(){
		var self = this;
		self.scrollTimer = setInterval(function(){
			self.smoothScroll();
		}, self.config.speed);
	},
	/**
	 * 停止滚动
	 */
	stop:function(){
		clearInterval(this.scrollTimer);
	},
	/**
	 * @ignore		
	 */	
	mouseOver:function(){ this.scrollStop=true; },
	/**
	 * @ignore		
	 */	
	mouseOut:function(){ this.scrollStop=false; },
	/**
	 * @ignore		
	 */	
	smoothScroll:function(){
   		if(this.scrollStop){return;}

    	if(this.content2.get(0).offsetWidth <= this.scrollObj.get(0).scrollLeft){ 
    		this.scrollObj.get(0).scrollLeft -= this.content1.get(0).offsetWidth;
		}else{
			this.scrollObj.get(0).scrollLeft++;
		}
	}
}
